﻿Public Class man_investigadores
    Dim tituloVentna As String = "Mantenedor de investigadores"
    'flag(bandera)discrimina si es editar o agregar
    Dim flag As Integer = 0
    Dim qry As String
    Dim cadena As String
    Dim ultima_qry As String
    Dim qryOriginal As String = "select INVE_ID,INVE_NOMBRE,INVE_APELLIDO,INVE_RUT,INVE_CENTRO,INVE_TELEFONO,INVE_EMAIL,INVE_CIUDAD from investigadores"
    Public Sub actualizar()
        carga_lista(qryOriginal)
    End Sub
    Public Sub carga_lista(ByVal qry)
        ListView1.Items.Clear()
        ultima_qry = qry 'aca dejo la ultima ejecutada :D
        Dim dataset As New DataSet
        Consulta(qry, dataset)
        If dataset.Tables("data").Rows.Count = 0 Then
            'MsgBox("No hay Registros", MsgBoxStyle.Information, "Error")
        Else
            With dataset.Tables("data")
                For f As Integer = 0 To .Rows.Count - 1
                    Dim dato As New ListViewItem(.Rows(f).Item(0).ToString)
                    ' recorrer las columnas  
                    For c As Integer = 1 To .Columns.Count - 1
                        dato.SubItems.Add(.Rows(f).Item(c).ToString())
                    Next
                    ListView1.Items.Add(dato)
                Next
            End With

        End If
    End Sub

    Private Sub buscar_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        ValidarCaracteresEspeciales(e)
    End Sub

    Private Sub btn_buscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_buscar.Click
        Dim condicion As String

        condicion = " where"

        Select Case ComboBox1.SelectedIndex
            Case 0
                qry = cadena & condicion & " inve_nombre = '" & buscar.Text & "'"
                carga_lista(qry)

            Case 1
                qry = cadena & condicion & " inve_apellido = '" & buscar.Text & "'"
                carga_lista(qry)
            Case 2
                qry = cadena & condicion & " inve_rut  =  '" & buscar.Text & "'"
                carga_lista(qry)
            Case 3
                qry = cadena & condicion & " inve_centro =  '" & buscar.Text & "'"
                carga_lista(qry)
            Case 4
                qry = cadena & condicion & " inve_ciudad =  '" & buscar.Text & "'"
                carga_lista(qry)
        End Select
    End Sub

    Private Sub buscar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buscar.TextChanged
        If CheckBox1.Checked = True Then
            Dim condicion As String

            condicion = " where"

            Select Case ComboBox1.SelectedIndex
                Case 0
                    qry = cadena & condicion & " inve_nombre like '" & "%" & buscar.Text & "%" & "'"
                    carga_lista(qry)

                Case 1
                    qry = cadena & condicion & " inve_apellido like  '" & "%" & buscar.Text & "%" & "'"
                    carga_lista(qry)
                Case 2
                    qry = cadena & condicion & " inve_rut like  '" & "%" & buscar.Text & "%" & "'"
                    carga_lista(qry)
                Case 3
                    qry = cadena & condicion & " inve_centro like '" & "%" & buscar.Text & "%" & "'"
                    carga_lista(qry)
                Case 4
                    qry = cadena & condicion & " inve_ciudad like '" & "%" & buscar.Text & "%" & "'"
                    carga_lista(qry)
            End Select
        End If
    End Sub

    Private Sub btn_actualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_actualizar.Click
        carga_lista("select INVE_ID,INVE_NOMBRE,INVE_APELLIDO,INVE_RUT,INVE_CENTRO,INVE_TELEFONO,INVE_EMAIL,INVE_CIUDAD from investigadores")
    End Sub

    Private Sub btn_exportar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_exportar.Click
        Dim dataset As New DataSet
        dataset = Consulta(ultima_qry, dataset)
        exel(dataset, ProgressBar1)
        'registro movimiento
        registro("exportar", login.usuario.id, main.modulo.investigador)
    End Sub

    Private Sub man_investigadores_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListView1.View = View.Details
        carga_lista("select INVE_ID,INVE_NOMBRE,INVE_APELLIDO,INVE_RUT,INVE_CENTRO,INVE_TELEFONO,INVE_EMAIL,INVE_CIUDAD from investigadores")
        cadena = "select INVE_ID,INVE_NOMBRE,INVE_APELLIDO,INVE_RUT,INVE_CENTRO,INVE_TELEFONO,INVE_EMAIL,INVE_CIUDAD from investigadores"
        ComboBox1.SelectedIndex = 0
        With Me
            .Icon = (My.Resources.favicon)
            .Text = tituloVentna
        End With
    End Sub

    Private Sub btn_cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_cerrar.Click
        Me.Close()
    End Sub

    Private Sub btn_eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_eliminar.Click
        Dim qry As String
        Dim row As Integer
        Dim id As String
        Try
            row = ListView1.SelectedIndices.Item(0)
            id = ListView1.Items.Item(row).SubItems(0).Text
            qry = "delete from investigadores where inve_id='" & id & "'"
            If MsgBox("¿Esta seguro de eliminar este registro?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                Try
                    EjecutaConsulta(qry)
                    'registro movimiento
                    registro("eliminar", login.usuario.id, main.modulo.investigador)
                    carga_lista("select INVE_ID,INVE_NOMBRE,INVE_APELLIDO,INVE_RUT,INVE_CENTRO,INVE_TELEFONO,INVE_EMAIL,INVE_CIUDAD from investigadores")

                Catch ex As Exception
                    MsgBox("Error en DB")
                End Try
            End If
        Catch
            MsgBox("Debe seleccionar un registro")
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        reporte.Owner = Me
        Dim rp As New CrystalReport1
        reporte.carga_info(ultima_qry, rp)
    End Sub

    Private Sub btn_editar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_editar.Click
        Dim editar As New nuevo_investigador
        Dim id As Integer
        Dim row As Integer
        'Try
        row = ListView1.SelectedIndices.Item(0)
        id = ListView1.Items.Item(row).SubItems(0).Text
        editar.setid(id)
        editar.ShowDialog()
        ' Catch
        ' MsgBox("Debe escoger un registro")
        ' End Try
    End Sub
End Class